package com.sci99.ex.vm;

import java.util.HashSet;
import java.util.List;

import org.apache.log4j.Logger;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.TreeModel;
import org.zkoss.zul.Window;

import com.sci99.ex.model.Course;
import com.sci99.ex.model.Department;
import com.sci99.ex.model.DptModel;
import com.sci99.ex.model.DptNode;
import com.sci99.ex.service.CourseAssignService;
import com.sci99.ex.service.CourseService;
import com.sci99.ex.service.DptService;
import com.sci99.ex.util.TreeUtil;

/**
 * 部门课程分配
 * 
 * @author xuzhen
 * 
 */
public class DepartmentCourseAssignVM {
	static Logger log = Logger.getLogger(DepartmentCourseAssignVM.class);

	TreeModel<DptNode> treeModel;
	HashSet<DptNode> selNodes;
	DptNode selNode;
	List<Course> courseList;
	List<Course> assignList;
	Course selAssign;
	Course selCourse;
	String courseCode;
	String courseName;

	public TreeModel<DptNode> getTreeModel() {
		if (treeModel == null) {
			List<Department> l;
			try {
				l = DptService.getDepartment();
			} catch (Exception e) {
				log.error("", e);
				return null;
			}
			treeModel = new DptModel(TreeUtil.build(l));
		}
		return treeModel;
	}

	public Department initTreeModel() {
		Department root = new Department();
		return root;
	}

	public HashSet<DptNode> getSelNodes() {
		return selNodes;
	}

	public void setSelNodes(HashSet<DptNode> selNodes) {
		this.selNodes = selNodes;
	}

	public DptNode getSelNode() {
		return selNode;
	}

	public void setSelNode(DptNode selNode) {
		this.selNode = selNode;
	}

	public List<Course> getCourseList() {
		return courseList;
	}

	public void setCourseList(List<Course> courseList) {
		this.courseList = courseList;
	}

	public List<Course> getAssignList() {
		return assignList;
	}

	public void setAssignList(List<Course> assignList) {
		this.assignList = assignList;
	}

	public Course getSelAssign() {
		return selAssign;
	}

	public void setSelAssign(Course selAssign) {
		this.selAssign = selAssign;
	}

	public Course getSelCourse() {
		return selCourse;
	}

	public void setSelCourse(Course selCourse) {
		this.selCourse = selCourse;
	}

	public String getCourseCode() {
		return courseCode;
	}

	public void setCourseCode(String courseCode) {
		this.courseCode = courseCode;
	}

	public String getCourseName() {
		return courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	@Command
	@NotifyChange("assignList")
	public void assign(@BindingParam("win") Window cWin) {
		if (selCourse == null) {
			Messagebox.show("请选择课程");
			return;
		}
		try {
			CourseAssignService.addDepartmentCourseAssign(selCourse.getId(),
					selNode.getIndex());
			assignList = CourseService.getCourseListByDepId(selNode.getIndex());
		} catch (Exception e) {
			log.error("", e);
			Messagebox.show(Msg.DbFail);
		}
		cWin.setVisible(false);
	}

	@Command
	public void add(@BindingParam("win") Window cWin) {
		if (selNode == null) {
			Messagebox.show("请选择部门");
			return;
		}
		cWin.doModal();
	}

	@Command
	@NotifyChange("assignList")
	public void del() {
		if (selAssign == null) {
			return;
		}
		if (Messagebox.show(Msg.CfmDel, null,
				Messagebox.OK + Messagebox.CANCEL, Messagebox.QUESTION) != Messagebox.OK)
			return;
		try {
			CourseAssignService.delDepartmentCourseAssign(selAssign.getId(),
					selNode.getIndex());
			assignList = CourseService.getCourseListByDepId(selNode.getIndex());
		} catch (Exception e) {
			log.error("", e);
		}
	}

	@Command
	public void close(@BindingParam("win") Window cWin) {
		cWin.setVisible(false);
	}

	@Command
	@NotifyChange("assignList")
	public void select() {
		try {
			assignList = CourseService.getCourseListByDepId(selNode.getIndex());
		} catch (Exception e) {
			log.error("", e);
			Messagebox.show(Msg.DbFail);
		}
	}

	@Command
	@NotifyChange("courseList")
	public void schCourse() {
		try {
			courseList = CourseService.getCourseList(courseName);
		} catch (Exception e) {
			log.error("", e);
			Messagebox.show(Msg.DbFail);
		}
	}
}
